﻿using System;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

namespace ClipPlayer
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private bool isBetweenTrack = false;


        private void buttonPrev_Click(object sender, RoutedEventArgs e)
        {
            buttonPrev.IsEnabled = false;
            //
            if (0 != filesToPlay.Count())
            {
                stat.incrementSkipped(System.IO.Path.GetFileName(filesToPlay[currentPlaying].ResolvedFile));
                if (currentPlaying > 0)
                {
                    volumeToZero();
                    isBetweenTrack = true;
                    //
                    stopPlaying();
                    currentPlaying--;
                    playCurrent();
                    //
                    isBetweenTrack = false;
                    volumeToMax();
                }
                else
                {
                    stopPlaying();
                }
            }
            //
            buttonPrev.IsEnabled = true;
        }

        private void buttonPlay_Click(object sender, RoutedEventArgs e)
        {
            buttonPlay.IsEnabled = false;
            //
            if (vlc.playlist.isPlaying)
            {
                stopPlaying();
            }
            else
            {
                if (0 == filesToPlay.Count())
                {
                    stopPlaying();
                }
                else
                {
                    playCurrent();
                }
            }
            //
            buttonPlay.IsEnabled = true;
        }

        private void buttonNext_Click(object sender, RoutedEventArgs e)
        {
            buttonNext.IsEnabled = false;
            //
            if (0 != filesToPlay.Count())
            {
                stat.incrementSkipped(System.IO.Path.GetFileName(filesToPlay[currentPlaying].ResolvedFile));
                if ((currentPlaying + 1) < filesToPlay.Count())
                {
                    volumeToZero();
                    isBetweenTrack = true;
                    //
                    stopPlaying();
                    currentPlaying++;
                    playCurrent();
                    //
                    isBetweenTrack = false;
                    volumeToMax();
                }
                else
                {
                    stopPlaying();
                }
            }
            //
            buttonNext.IsEnabled = true;
        }

        private void axVLCPlugin21_MediaPlayerTimeChanged(object sender, AxAXVLC.DVLCEvents_MediaPlayerTimeChangedEvent e)
        {
            Dispatcher.Invoke(new Action(() => progressBar1.Value = (int)(vlc.input.Position * 100.0)));
        }

        private void win1_PreviewMouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            if (!vlc.playlist.isPlaying)
            {
                return;
            }
            r2.IsEnabled = false;
            //
            if (this.WindowState != System.Windows.WindowState.Maximized)
            {
                EnterFullScreenMode();
            }
            else if (this.WindowState != System.Windows.WindowState.Normal)
            {
                LeaveFullScreenMode();
            }
            //
            r2.IsEnabled = true;
        }




        private bool isRandom = false;
        private bool setRandom(bool isRandomNewValue)
        {
            bool currentRabdom = isRandom;
            if (isRandomNewValue)
            {
                isRandom = true;
                buttonRandom.ToolTip = "Random";
                SetButtonImage(buttonRandom, "Random");
            }
            else
            {
                isRandom = false;
                buttonRandom.ToolTip = "Sequential";
                SetButtonImage(buttonRandom, "NoRandom");
            }
            //
            return currentRabdom;
        }

        private void buttonRandom_Click(object sender, RoutedEventArgs e)
        {
            setRandom(!isRandom);
        }

        private void progressBar1_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (!vlc.playlist.isPlaying)
            {
                return;
            }
            progressBar1.IsEnabled = false;
            volumeToZero();
            Point p = e.GetPosition(progressBar1);
            vlc.input.Position = p.X / (progressBar1.Maximum - progressBar1.Minimum);
            volumeToMax();
            progressBar1.IsEnabled = true;
        }

        private void buttonDelete_Click(object sender, RoutedEventArgs e)
        {
            buttonDelete.IsEnabled = false;
            //
            int toDelete = currentPlaying;
            if (0 != filesToPlay.Count())
            {
                volumeToZero();
                if ((currentPlaying + 1) < filesToPlay.Count())//ostatni
                {
                    isBetweenTrack = true;
                }
                //
                stopPlaying();
                //
                if ((currentPlaying + 1) < filesToPlay.Count())//ostatni
                {
                    currentPlaying++;
                    playCurrent();
                }
                //
                isBetweenTrack = false;
                volumeToMax();
                //
                stat.incrementDeleted(System.IO.Path.GetFileName(filesToPlay[toDelete].ResolvedFile));
                deleteFileSafe(filesToPlay[toDelete].OriginalFile);
                if (filesToPlay[toDelete].OriginalFile != filesToPlay[toDelete].ResolvedFile) deleteFileSafe(filesToPlay[toDelete].ResolvedFile);
                //
            }
            //
            buttonDelete.IsEnabled = true;
        }


        private void buttonNote_Click(object sender, RoutedEventArgs e)
        {
            buttonNote.IsEnabled = false;
            //
            int toNote = currentPlaying;
            if (0 != filesToPlay.Count())
            {
                try
                {
                    using (System.IO.StreamWriter sw = System.IO.File.AppendText("notes.txt"))
                    {
                        sw.WriteLine(string.Format("{0} [{1}]{2}", DateTime.Now, filesToPlay[toNote].ResolvedFile, Environment.NewLine));
                        sw.Flush();
                    }
                }
                catch { }
            }
            //
            buttonNote.IsEnabled = true;
        }

        private void buttonLike_Click(object sender, RoutedEventArgs e)
        {
            buttonLike.IsEnabled = false;
            //
            int toNote = currentPlaying;
            if (0 != filesToPlay.Count())
            {
                try
                {
                    using (System.IO.StreamWriter sw = System.IO.File.AppendText("likes.txt"))
                    {
                        sw.WriteLine(string.Format("{0} [{1}]{2}", DateTime.Now, filesToPlay[toNote].ResolvedFile, Environment.NewLine));
                        sw.Flush();
                    }
                }
                catch { }
            }
            //
            buttonLike.IsEnabled = true;
        }

    }//class
}//namespace
